#include "mainmanager.h"
#include <QFileInfo>
#include <QtConcurrent>
#include <QDir>
#include <QStringList>
#include "common.h"
#include "httprequest.h"


MainManager::MainManager(QObject *parent)
    :QObject(parent) {
    m_httpPost = new HttpRequest();
    m_httpPost->fillDatas(HttpRequest::kDongFangCaiFuRequest, "https://qt.gtimg.cn/");
    connect(m_httpPost, &HttpRequest::signalStockInfomation, this, &MainManager::signalStockInfomation, Qt::QueuedConnection);
    QString fromer = "2427904340@qq.com";
    QString toer = "821984831@qq.com";
    QString smtpserver = "smtp.qq.com";
    QString subject = "Test title ";
    QString mailmessage = "this is a test ";
    m_mailer = new jwsmtp::mailer(toer.toStdString().c_str(), fromer.toStdString().c_str(), subject.toStdString().c_str(), mailmessage.toStdString().c_str(), smtpserver.toStdString().c_str(), jwsmtp::mailer::SMTP_PORT, false);
    //mail_.setsender(fromer.toStdString().c_str());
    m_mailer->username(fromer.toStdString().c_str());
    m_mailer->password("caodan520");
    m_mailer->send();

    //m_mailer->operator();
    qDebug() << QString::fromStdString(m_mailer->response());


    // add another recipient (carbon copy)
    //mail.addrecipient("someoneelse@somewhere.net", mailer::Cc);

    // set a new smtp server! This is the same as setting a nameserver.
    // this depends on the constructor call. i.e. in the constructor
    // If MXLookup is true this is a nameserver
    // If MXLookup is false this is an smtp server
    //mail.setserver("mail.somewherefun.com");
    // same again except using an IP address instead.
    //mail.setserver("192.168.0.1");

    // boost::thread thrd(mail);
    // thrd.join(); // optional
    // or:-


//    m_timer.setInterval(1000);
//    m_timer.start();
//    connect(&m_timer, &QTimer::timeout, this, [this](){
//        getStockInfo("603518");
//    });


}

MainManager::~MainManager() {
    m_httpPost->deleteLater();
}

void MainManager::quitApp()
{
    QApplication::quit();
}

void MainManager::getStockInfo(QString code)
{
    if(code.isEmpty())
        return;
    if(code.size()> 2 && (code.at(0) == '5' || code.at(0) == '6')) {
        code = "sh" + code;
    }
    else
        code = "sz" + code;
    m_httpPost->getStockInfo(code);
}

void MainManager::declareQML()
{
    qmlRegisterType<MainManager>("MainManager", 1, 0, "MainManager");
}

QString MainManager::getApplicationDirPath()
{
    return QApplication::applicationDirPath();
}

QString MainManager::getTitleName()
{
    return common::getApplicationName();
}

